<?php
interface BillInterface {
	public function bill($accountId, $amout);
}

class OrderProcessor {
	public function __construct(BillInterface $biller) {
		$this->biller = $biller;
	}

	public function process(Order $order) {
		// $recent = $this->getRecentOrderCount($order);
		$recent = $order->getRecentOrderCount($order->account);	//将职责放到对应的order中
		if ($recent > 0) {
			throw new \Exception('重复订单');
		}
		$this->biller->bill($order->account->id, $order->amount);
		Db::table('orders')->insert([
			'account' => $order->account->id,
			'amount' => $order->amount,
			'created_at' => Carbon::now()
		]);
	}

	// 职责不对，提取到新的类中
	protected function getRecentOrderCount(Order $order)
	{
		$timestamp = Carbon::now()->subMinutes(5);
		return Db::table('orders')
			->where('account', $order->account->id)
			->where('created_at', '>=', $timestamps)
			->count();
	}
}

